/**
 * Created by zy on 2016/6/16.
 * 缺陷评价 - 推送设置 - 系统设置
 */


;(function ($, window, document, undefined) {

    /**
     * 获取设置数据 - start
     */
    var baseData = function () {
        var temp = '';
        DyyBase.ajax(sysApi.url_settings_threshold(), {type: 'DEFECT_EVALUATION'}, function (dataset) {
            temp = dataset.result;
        }, {async: false});
        return temp;
    }();
    /**
     * 获取设置数据 - end
     */

    /**
     * 获取设置数据 - start
     */
    // 高后果区
    var area_level = function () {
        var temp = $.trim($('#JQ_data_area_level').html());
        temp = eval('(' + temp + ')');
        //temp.shift();
        return DyyBase.getSelect(temp);
    }();

    // 角色列表
    var role_list = function () {
        var temp = $.trim($('#JQ_data_role_list').html());
        temp = eval('(' + temp + ')');
        return temp;
    }();

    // 角色对应
    var role_map = function () {
        var temp = $.trim($('#JQ_data_role_map').html());
        temp = eval('(' + temp + ')');
        return temp;
    }();

    // 高后果区 01
    var $area_level_01 = $('#JQ_area_level_01').select3({
        search: false,
        allowClear: false,
        data: area_level
    });
    // 高后果区 02
    var $area_level_02 = $('#JQ_area_level_02').select3({
        search: false,
        allowClear: false,
        data: area_level
    });
    // 高后果区 03
    var $area_level_03 = $('#JQ_area_level_03').select3({
        search: false,
        allowClear: false,
        data: area_level
    });
    /**
     * 获取设置数据 - end
     */

    //console.log(baseData);

    /**
     * 插件化页面复选下拉框 - start
     */
    var $list_box_01;
    var $immediate_repair = $('#JQ_immediate_repair');
    var $metal_loss = $('#JQ_metal_loss');
    var $pit = $('#JQ_pit');
    var $length = $('#JQ_length');
    var $width = $('#JQ_width');
    /**
     * 插件化页面复选下拉框 - end
     */


    /**
     *  初始化页面数据 - start
     */
    function initPage() {
        // 立即维修和ERF信息设置
        $immediate_repair.val(baseData.DEFECT_IMMEDIATE_REPAIR.thresholdContentJson.erf);

        // 金属损失
        $metal_loss.val(baseData.DEFECT_METAL_LOSS.thresholdContentJson.erf);
        $area_level_01.fit(baseData.DEFECT_METAL_LOSS.thresholdContentJson.areaLevel);

        // 凹陷
        $pit.val(baseData.DEFECT_PIT.thresholdContentJson.depth);
        $area_level_02.fit(baseData.DEFECT_PIT.thresholdContentJson.areaLevel);

        // 环焊缝异常
        $length.val(baseData.DEFECT_GIRTH_WELD.thresholdContentJson.length);
        $width.val(baseData.DEFECT_GIRTH_WELD.thresholdContentJson.width);
        $area_level_03.val(baseData.DEFECT_GIRTH_WELD.thresholdContentJson.areaLevel);

        // 维修维护项目启动、关闭
        $list_box_01 = $('#JQ_checkbox_list_01').DropCheckbox({
            list: role_list,
            data: baseData.DEFECT_REPAIR_TASKS.roleId,
            roleMap: role_map
        });
    }

    initPage();
    /**
     *  初始化页面数据 - end
     */


    /**
     * 注入数据 - start
     */
    function pushData() {
        // 立即维修和ERF信息设置
        baseData.DEFECT_IMMEDIATE_REPAIR.thresholdContentJson.erf  = $immediate_repair.val();

        // 金属损失
        baseData.DEFECT_METAL_LOSS.thresholdContentJson.erf = $metal_loss.val();
        baseData.DEFECT_METAL_LOSS.thresholdContentJson.areaLevel = $area_level_01.val();

        // 凹陷
        baseData.DEFECT_PIT.thresholdContentJson.depth = $pit.val();
        baseData.DEFECT_PIT.thresholdContentJson.areaLevel = $area_level_02.val();

        // 环焊缝异常
        baseData.DEFECT_GIRTH_WELD.thresholdContentJson.length = $length.val();
        baseData.DEFECT_GIRTH_WELD.thresholdContentJson.width = $width.val();
        baseData.DEFECT_GIRTH_WELD.thresholdContentJson.areaLevel = $area_level_03.val();

        // 维修维护项目启动、关闭
        baseData.DEFECT_REPAIR_TASKS.roleId = $list_box_01.spans.join(',');

        return baseData;
    }

    /**
     * 注入数据 - end
     */


    /**
     * 校验数据重复性有效性 - start
     */
    function checkData(){
        //
    }
    /**
     * 校验数据重复性有效性 - end
     */


    // 保存更改设置
    $('#JQ_save_settings').click(function () {
        var verify = $('#JQ_defect_form').verify();
        var $this = $(this);
        if(verify){
            if ($this.attr('data-status') === 'true') {
                $this.attr('data-status', 'false');
                var data = JSON.stringify(pushData());
                DyyBase.ajax(sysApi.url_settings_defect_save(), data, function (dataset) {
                    $this.attr('data-status', 'true');
                    if (dataset.bool) {
                        $this.MSG({
                            type: 'success',
                            content: '缺陷评价设置成功！'
                        });
                    } else {
                        $this.MSG({
                            type: 'error',
                            content: '缺陷评价设置失败！'
                        });
                    }
                }, {headers : {"Content-Type" : "application/json"}});
            }
        }
    });

    // 恢复默认设置
    $('#JQ_save_defaults').click(function () {
        var $this = $(this);
        if ($this.attr('data-status') === 'true') {
            $this.attr('data-status', 'false');
            DyyBase.ajax(sysApi.url_settings_defect_cancel(), {}, function (dataset) {
                $this.attr('data-status', 'true');
                if (dataset.bool) {
                    $(this).MSG({
                        type: 'success',
                        content: '缺陷评价恢复默认设置成功！',
                        closeBtnExist: false,
                        yesBtnAct: DyyBase.pageReload
                    });
                } else {
                    $(this).MSG({
                        type: 'error',
                        closeBtnExist: false,
                        content: '缺陷评价恢复默认设置失败！'
                    });
                }
            });
        }
    });

})(jQuery, window, document);